#include "Menu.h"

/***************************************************************************************
* function name: void start()
* The Input: None
* The output: None
* The Function operation: Asks for user's options and performs them, using Inventory
* 						  object's method
***************************************************************************************/
void Menu::start() throw(bad_alloc) {

	short option; // used to store user's option
	bool keepRunning = true; // used to indicate whether should keep looping or not

	while (keepRunning) {
		cin >> option; // get user's option
		switch(option) {

		// add an item is chosen
		case ADD_ITEM: {
			short itemNumber;
			string itemName;
			cin >> itemNumber >> itemName; // get new item's number and name
			try { // try adding new item
				m_inventoty.addItem(itemNumber, itemName);
			}
			catch (ItemExistsError& invalidItem) { // could not add since item already exist
				cout << invalidItem.what() << endl; // print error message
			}
			break;
		} // case ADD_ITEM

		// order stock is chosen
		case ORDER_STOCK: {
			short itemNumber;
			unsigned short amount;
			cin >> itemNumber >> amount; // get item's number and amount to be ordered
			try {
				m_inventoty.orderStock(itemNumber, amount);
			}
			catch (NoItemError& invalidItem) { // could not order since item does not exist
				cout << invalidItem.what() << endl; // print error message
			}
			break;
		} // case ORDER_STOCK

		// receive an order is chosen
		case RECEIVE_ORDER: {
			short orderNum;
			unsigned short amount;
			cin >> orderNum >> amount; // get order's number and amount received
			m_inventoty.recieveStock(orderNum, amount); // receive stock
			break;
		} // case RECEIVE_ORDER

		// sell an item is chosen
		case SELL_ITEM: {
			short itemNumber;
			unsigned short amount;
			cin >> itemNumber >> amount; // get item's number and amount to be sold
			try {
				m_inventoty.sellItem(itemNumber, amount); // print error message
			}
			catch (NoItemError& invalidItem) { // could not sale since there is no item with given number
				cout << invalidItem.what() << endl;
			}
			catch (TooManyOrderedError& error) { // could not sale since not enough items in stock
				cout << error. what() << endl;
			}
			break;
		} // case SELL_ITEM

		// printing sale documentation is chose
		case PRINT_SALES_DOC: {
			m_inventoty.printSaleDoc(); // print sale documentation
			break;
		}

		// terminate loop in order to exit program
		case EXIT_PROGRAM: {
			keepRunning = false; // set keep running flag to false and end loop
			break;
		}

		default: { // default case
			break;
		}

		} // switch(option)
	} // while (keepRunning)
}
